﻿using System;
using System.Collections.Generic;
using System.Linq;

// 订单明细类
class OrderDetails
{
    public string ProductName { get; set; }
    public double Price { get; set; }
    public int Quantity { get; set; }

    public OrderDetails(string productName, double price, int quantity)
    {
        ProductName = productName;
        Price = price;
        Quantity = quantity;
    }

    public override bool Equals(object obj)
    {
        if (obj is OrderDetails details)
        {
            return ProductName == details.ProductName && Quantity == details.Quantity && Price == details.Price;
        }
        return false;
    }

    public override int GetHashCode()
    {
        return HashCode.Combine(ProductName, Price, Quantity);
    }

    public override string ToString()
    {
        return $"商品名称: {ProductName}, 单价: {Price}, 数量: {Quantity}";
    }
}

// 订单类
class Order
{
    public string OrderId { get; set; }
    public string Customer { get; set; }
    public List<OrderDetails> OrderDetails { get; set; }
    public double TotalAmount => OrderDetails.Sum(d => d.Price * d.Quantity);

    public Order(string orderId, string customer, List<OrderDetails> orderDetails)
    {
        OrderId = orderId;
        Customer = customer;
        OrderDetails = orderDetails;
    }

    public override bool Equals(object obj)
    {
        return obj is Order order &&
               OrderId == order.OrderId &&
               Customer == order.Customer &&
               OrderDetails.SequenceEqual(order.OrderDetails);
    }

    public override int GetHashCode()
    {
        return HashCode.Combine(OrderId, Customer, OrderDetails);
    }

    public override string ToString()
    {
        string details = string.Join("\n  ", OrderDetails.Select(d => d.ToString()));
        return $"订单号: {OrderId}, 客户: {Customer}, 总金额: {TotalAmount}\n  {details}";
    }
}

// 订单服务类
class OrderService
{
    private List<Order> orders = new List<Order>();

    public void AddOrder(Order order)
    {
        if (orders.Contains(order))
        {
            throw new Exception("订单已存在，无法添加。");
        }
        orders.Add(order);
    }

    public void DeleteOrder(string orderId)
    {
        var order = orders.FirstOrDefault(o => o.OrderId == orderId);
        if (order == null)
        {
            throw new Exception($"未找到订单号为 {orderId} 的订单，删除失败。");
        }
        orders.Remove(order);
    }

    public void ModifyOrder(Order newOrder)
    {
        var index = orders.FindIndex(o => o.OrderId == newOrder.OrderId);
        if (index == -1)
        {
            throw new Exception($"未找到订单号为 {newOrder.OrderId} 的订单，修改失败。");
        }
        orders[index] = newOrder;
    }

    public List<Order> QueryOrdersByOrderId(string orderId)
    {
        return orders.Where(o => o.OrderId == orderId).OrderBy(o => o.TotalAmount).ToList();
    }

    public List<Order> QueryOrdersByProductName(string productName)
    {
        return orders.Where(o => o.OrderDetails.Any(d => d.ProductName == productName))
                     .OrderBy(o => o.TotalAmount).ToList();
    }

    public List<Order> QueryOrdersByCustomer(string customer)
    {
        return orders.Where(o => o.Customer == customer).OrderBy(o => o.TotalAmount).ToList();
    }

    public List<Order> QueryOrdersByTotalAmount(double amount)
    {
        return orders.Where(o => o.TotalAmount == amount).OrderBy(o => o.TotalAmount).ToList();
    }

    public void SortOrders()
    {
        orders = orders.OrderBy(o => o.OrderId).ToList();
    }

    public void SortOrders(Func<Order, object> sortExpression)
    {
        orders = orders.OrderBy(sortExpression).ToList();
    }

    public List<Order> GetAllOrders()
    {
        return orders;
    }
}

class Program
{
    static void Main()
    {
        OrderService orderService = new OrderService();

        // 添加订单
        try
        {
            var orderDetails1 = new List<OrderDetails>
            {
                new OrderDetails("商品A", 10, 2),
                new OrderDetails("商品B", 20, 1)
            };
            var order1 = new Order("001", "客户甲", orderDetails1);
            orderService.AddOrder(order1);

            var orderDetails2 = new List<OrderDetails>
            {
                new OrderDetails("商品C", 30, 1)
            };
            var order2 = new Order("002", "客户乙", orderDetails2);
            orderService.AddOrder(order2);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }

        // 查询订单
        var queryResult = orderService.QueryOrdersByCustomer("客户甲");
        Console.WriteLine($"客户甲的订单有：");
        foreach (var order in queryResult)
        {
            Console.WriteLine(order);
        }

        // 修改订单
        try
        {
            var newOrderDetails = new List<OrderDetails>
            {
                new OrderDetails("商品A", 10, 3)
            };
            var newOrder = new Order("001", "客户甲", newOrderDetails);
            orderService.ModifyOrder(newOrder);
            Console.WriteLine("订单修改成功，修改后的订单信息如下：");
            var modifiedOrder = orderService.QueryOrdersByOrderId("001").FirstOrDefault();
            if (modifiedOrder != null)
            {
                Console.WriteLine(modifiedOrder);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }

        // 删除订单
        try
        {
            orderService.DeleteOrder("001");
            Console.WriteLine("订单删除成功");
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }

        // 排序订单
        orderService.SortOrders();
        Console.WriteLine("订单已按订单号排序，排序后的订单信息如下：");
        var sortedOrders = orderService.GetAllOrders();
        foreach (var order in sortedOrders)
        {
            Console.WriteLine(order);
        }
    }
}
